<template>
	<view class="container">
		<view>
			<view class="uni-form-item">
				<text class="login_label">个人设置</text>
			</view>
			<!-- 			<view style="font-size: 60rpx;">
				<strong>个人设置</strong>
			</view> -->

			<view class="password">
				<text>密码修改 (不填则不修改)</text>
				<view class="pwd">
					<input class="uni-input" v-model="password" :password="!showPassword" style="width: 100%;"
						placeholder="不填则不修改" />
					<!-- 显示或隐藏密码 -->
					<image @click="()=>{showPassword=!showPassword;}"
						:src="showPassword?'/static/pwd/eye-open.png':'/static/pwd/eye-close.png'"
						style="position: absolute; z-index: 2;  height: 60rpx; width: 60rpx; margin-top: 20rpx; right: 20rpx;">
					</image>
				</view>
			</view>

			<view class="username">
				<text>真实名称</text><input class="uni-input" v-model="trueName" />
			</view>
			<view class="password">
				<text>手机号</text><input class="uni-input" v-model="phone" />
			</view>
			<view class="password">
				<text>邮箱</text><input class="uni-input" v-model="email" />
			</view>
			<view class="btn" style="margin-bottom: 124rpx;">
				<button type="primary" class="btn-login2" @click="updateUser">保存信息</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';

	export default {
		data() {
			return {
				showPassword: false,
				trueName: '',
				password: "",
				phone: "",
				email: "",
			};
		},
		computed: {
			...mapState('m_user', ['userinfo']),
		},
		onLoad() {
			if (this.userinfo) {
				this.create()
			}
		},
		methods: {
			...mapMutations('m_user', ['updateAddress', 'updateUserInfo', 'updateToken']),
			create() {
				// if (this.userinfo.password) {
				// 	this.password = this.userinfo.password
				// }
				if (this.userinfo.trueName) {
					this.trueName = this.userinfo.trueName
				}
				if (this.userinfo.phone) {
					this.phone = this.userinfo.phone
				}
				if (this.userinfo.email) {
					this.email = this.userinfo.email
				}
			},
			async updateUser() {
				if (this.check() == null) return;
				const res = await uni.$dapi.putUserInfo({
					id: this.userinfo.id,
					password: this.password,
					phone: this.phone,
					trueName: this.trueName,
					email: this.email
				})
				if (res === true) {

					const user = await uni.$dapi.getUserInfo();
					console.log(user);
					if (user == null) return;
					// this.updateUserInfo(user)

					setTimeout(() => {

						uni.navigateBack({
							delta: 1, //返回层数，2则上上页
						})
						uni.$showMsg("修改成功")
					}, 2000)
				}
			},
			check() {
				// if (this.username.length < 3 || this.username.length > 20) {
				// 	return uni.$showMsg("用户名需3-20个字符");
				// }
				if (this.password !== "" && (
						this.password.length < 3 || this.password.length > 20)) {
					return uni.$showMsg("密码需3-20个字符");
				}
				// if (this.cPassword.length < 3 || this.cPassword.length > 20) {
				// 	return uni.$showMsg("确认密码需3-20个字符");
				// }
				// if (this.password !== this.cPassword) {
				// 	return uni.$showMsg("确认密码不一致");
				// }
				// if (this.capt.length !== 5) {
				// 	return uni.$showMsg("验证码格式不正确");
				// }
				const reg = /^1[3456789]\d{9}$/;

				if (this.phone.length < 5 || !reg.test(this.phone)) {
					return uni.$showMsg("手机号格式不正确");
				}
				// if (this.smsCapt.length !== 6) {
				// 	return uni.$showMsg("手机验证码格式不正确");
				// }

				// if (this.capt.toLocaleLowerCase() !== this.captKey) {
				// 	return uni.$showMsg("验证码错误")
				// }
				return true;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: fit-content;
		padding: 20rpx 40rpx;
		// margin: 200rpx 20rpx;
		text-align: center;
		background-color: #c7d5cb;

		.username {
			margin-top: 30rpx;
		}

		.password {
			margin-top: 30rpx;

			.pwd {
				margin-top: 10rpx;
				display: flex;
				flex-direction: row;
				position: relative;
			}
		}

		.capt {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 30rpx;

			.capt-left {
				.capt-text {
					white-space: nowrap;
				}
			}

			.img {
				height: 100rpx;
				width: 300rpx;
				margin-top: 30rpx;
			}

		}

		.login_label {
			color: #ac1032;
			margin: 0 auto;
			font-size: 60rpx;
		}

		.uni-input {
			margin-top: 10rpx;
			background-color: #fff;
			height: 80rpx;
			text-align: left;
			padding-left: 10rpx;
			border-radius: 10rpx;
			font-size: 30rpx;
		}

		.uni-form-item {
			margin: 30rpx 0;
		}

		button {
			font-size: 30rpx;
		}

		.btn {
			// display: flex;
			// flex-direction: column;
			// justify-content: center;
			// align-items: center;
			margin-top: 200rpx;
			text-align: center;

			.btn-login {
				width: 90%;
				border-radius: 100px;
				margin: 15px auto;
				background-color: #00e5c3;
			}

			.btn-login2 {
				width: 90%;
				border-radius: 100px;
				margin: 15px auto;
				background-color: #C00000;
			}
		}

	}
</style>